package com.link.base.tencent.youtu.util;

import com.alibaba.fastjson.JSONObject;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.HttpRequestUtil;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * 人脸识别相关工具类
 *
 * @author ouyangzhenqiang
 * @date 2018/12/29
 */
public class FaceUtil {

    /**
     * 人脸检索
     * 本接口用于对一张待识别的人脸图片，在一个或多个 group 中识别出最相似的 Top5 person 作为其身份返回，返回的 Top5 中按照相似度从大到小排列
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     * @param authorization 签名
     * @param appId         接入项目的唯一标识
     * @param groupIdList     group列表
     * @param imgUrl        图片访问链接
     * @return
     * @throws Exception
     */
    public static String identify(String authorization, String appId, String groupIdList, String imgUrl) throws Exception {
        if (StringUtils.isBlank(authorization)) {
            throw new ServiceException("YOUTU-015");
        }
        if (StringUtils.isBlank(appId)) {
            throw new ServiceException("YOUTU-016");
        }
        if (StringUtils.isBlank(groupIdList)) {
            throw new ServiceException("YOUTU-017");
        }
        if (StringUtils.isBlank(imgUrl)) {
            throw new ServiceException("YOUTU-018");
        }
        String url = "https://service.image.myqcloud.com/face/identify";
        String param = "{\"appid\":\"" + appId + "\",\"group_ids\": [" + groupIdList + "] ,\"url\":\"" + imgUrl + "\"}";
        LCLogger.info().withMessageKey("identify").withMessage("identify.param=>" + param).flush();
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, authorization);
        LCLogger.info().withMessageKey("identify").withMessage("identify.result=>" + resultStr).flush();
        exception(resultStr);
        return resultStr;
    }

    /**
     * 获取项目下的所有分组
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     * @param authorization 签名
     * @param appId         接入项目的唯一标识
     * @return
     * @throws Exception
     */
    public static String getgroupids(String authorization, String appId) throws Exception {
        if (StringUtils.isBlank(authorization)) {
            throw new ServiceException("YOUTU-015");
        }
        if (StringUtils.isBlank(appId)) {
            throw new ServiceException("YOUTU-016");
        }
        String url = "https://recognition.image.myqcloud.com/face/getgroupids";
        String param = "{\"appid\":\"" + appId + "\"}";
        LCLogger.info().withMessageKey("getgroupids").withMessage("getgroupids.param=>" + param).flush();
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, authorization);
        LCLogger.info().withMessageKey("getgroupids").withMessage("getgroupids.result=>" + resultStr).flush();
        exception(resultStr);
        return resultStr;
    }

    /**
     * 获取分组下的所有person
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     * @param authorization 签名
     * @param appId         接入项目的唯一标识
     * @param group_id      分组Id
     * @return
     * @throws Exception
     */
    public static String getpersonids(String authorization, String appId, String group_id) throws Exception {
        if (StringUtils.isBlank(authorization)) {
            throw new ServiceException("YOUTU-015");
        }
        if (StringUtils.isBlank(appId)) {
            throw new ServiceException("YOUTU-016");
        }
        if (StringUtils.isBlank(group_id)) {
            throw new ServiceException("YOUTU-019");
        }
        String url = "https://recognition.image.myqcloud.com/face/getpersonids";
        String param = "{\"appid\":\"" + appId + "\",\"group_id\":\"" + group_id + "\"}";
        LCLogger.info().withMessageKey("getpersonids").withMessage("getpersonids.param=>" + param).flush();
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, authorization);
        LCLogger.info().withMessageKey("getpersonids").withMessage("getpersonids.result=>" + resultStr).flush();
        exception(resultStr);
        return resultStr;
    }

    /**
     * 获取person下的所有face
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     * @param authorization 签名
     * @param appId         接入项目的唯一标识
     * @param person_id     person Id
     * @return
     * @throws Exception
     */
    public static String getfaceids(String authorization, String appId, String person_id) throws Exception {
        if (StringUtils.isBlank(authorization)) {
            throw new ServiceException("YOUTU-015");
        }
        if (StringUtils.isBlank(appId)) {
            throw new ServiceException("YOUTU-016");
        }
        if (StringUtils.isBlank(person_id)) {
            throw new ServiceException("YOUTU-020");
        }
        String url = "https://recognition.image.myqcloud.com/face/getfaceids";
        String param = "{\"appid\":\"" + appId + "\",\"person_id\":\"" + person_id + "\"}";
        LCLogger.info().withMessageKey("getfaceids").withMessage("getfaceids.param=>" + param).flush();
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, authorization);
        LCLogger.info().withMessageKey("getfaceids").withMessage("getfaceids.result=>" + resultStr).flush();
        exception(resultStr);
        return resultStr;
    }

    /**
     * 创建个体
     * 创建一个 person，并将 person 放置到 group_ids 指定的组当中，不存在的 group_id 会自动创建
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     * @param authorization 签名
     * @param appId         接入项目的唯一标识
     * @param groupList     group列表
     * @param person_id
     * @param imgUrl        图片访问链接
     * @return
     * @throws Exception
     */
    public static String newperson(String authorization, String appId, String groupList, String person_id, String imgUrl) throws Exception {
        if (StringUtils.isBlank(authorization)) {
            throw new ServiceException("YOUTU-015");
        }
        if (StringUtils.isBlank(appId)) {
            throw new ServiceException("YOUTU-016");
        }
        if (StringUtils.isBlank(person_id)) {
            throw new ServiceException("YOUTU-020");
        }
        if (StringUtils.isBlank(groupList)) {
            throw new ServiceException("YOUTU-017");
        }
        if (StringUtils.isBlank(imgUrl)) {
            throw new ServiceException("YOUTU-018");
        }
        String url = "https://service.image.myqcloud.com/face/newperson";
        String param = "{\"appid\":\"" + appId + "\",\"group_ids\": [" + groupList + "]"
                + " ,\"person_id\":\"" + person_id + "\",\"url\": \"" + imgUrl + "\"}";
        LCLogger.info().withMessageKey("newperson").withMessage("newperson.param=>" + param).flush();
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, authorization);
        LCLogger.info().withMessageKey("newperson").withMessage("newperson.result=>" + resultStr).flush();
        exception(resultStr);
        return resultStr;
    }

    /**
     * 删除个体
     * 删除一个 person
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     * @param authorization 签名
     * @param appId         接入项目的唯一标识
     * @param person_id
     * @return
     * @throws Exception
     */
    public static String delperson(String authorization, String appId, String person_id) throws Exception {
        if (StringUtils.isBlank(authorization)) {
            throw new ServiceException("YOUTU-015");
        }
        if (StringUtils.isBlank(appId)) {
            throw new ServiceException("YOUTU-016");
        }
        if (StringUtils.isBlank(person_id)) {
            throw new ServiceException("YOUTU-020");
        }
        String url = "https://recognition.image.myqcloud.com/face/delperson";
        String param = "{\"appid\":\"" + appId + "\",\"person_id\":\"" + person_id + "\"}";
        LCLogger.info().withMessageKey("delperson").withMessage("delperson.param=>" + param).flush();
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, authorization);
        LCLogger.info().withMessageKey("delperson").withMessage("delperson.result=>" + resultStr).flush();
        exception(resultStr);
        return resultStr;
    }

    /**
     * 增加人脸
     * 将一组 face 加入到一个 person 中，一个 person 最多允许包含 20 个 face
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     * @param authorization 签名
     * @param appId         接入项目的唯一标识
     * @param person_id
     * @param urlList       图片访问链接列表
     * @return
     * @throws Exception
     */
    public static String addface(String authorization, String appId, String person_id, List<String> urlList) throws Exception {
        if (StringUtils.isBlank(authorization)) {
            throw new ServiceException("YOUTU-015");
        }
        if (StringUtils.isBlank(appId)) {
            throw new ServiceException("YOUTU-016");
        }
        if (StringUtils.isBlank(person_id)) {
            throw new ServiceException("YOUTU-020");
        }
        if (urlList.size() == 0) {
            throw new ServiceException("YOUTU-018");
        }
        String urls = "";
        for (int i = 0; i < urlList.size(); i++) {
            if (urls == "") {
                urls = "\"" + urlList.get(i) + "\"";
            } else {
                urls += ",\"" + urlList.get(i) + "\"";
            }
        }
        String url = "https://service.image.myqcloud.com/face/addface";
        String param = "{\"appid\":\"" + appId + "\",\"person_id\": \"" + person_id + "\",\"urls\": [" + urls + "]}";
        LCLogger.info().withMessageKey("addface").withMessage("addface.param=>" + param).flush();
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, authorization);
        LCLogger.info().withMessageKey("addface").withMessage("addface.result=>" + resultStr).flush();
        exception(resultStr);
        return resultStr;
    }

    /**
     * 删除人脸
     * 删除一个 person 下的 face ，包括特征、属性和 face id
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     * @param authorization 签名
     * @param appId         接入项目的唯一标识
     * @param person_id
     * @param faceIdList    列表
     * @return
     * @throws Exception
     */
    public static String delface(String authorization, String appId, String person_id, List<String> faceIdList) throws Exception {
        if (StringUtils.isBlank(authorization)) {
            throw new ServiceException("YOUTU-015");
        }
        if (StringUtils.isBlank(appId)) {
            throw new ServiceException("YOUTU-016");
        }
        if (StringUtils.isBlank(person_id)) {
            throw new ServiceException("YOUTU-020");
        }
        if (faceIdList.size() == 0) {
            throw new ServiceException("YOUTU-021");
        }
        String face_ids = "";
        for (int i = 0; i < faceIdList.size(); i++) {
            if (face_ids == "") {
                face_ids = "\"" + faceIdList.get(i) + "\"";
            } else {
                face_ids += ",\"" + faceIdList.get(i) + "\"";
            }
        }
        String url = "https://service.image.myqcloud.com/face/delface";
        String param = "{\"appid\":\"" + appId + "\",\"person_id\": \"" + person_id + "\",\"face_ids\": [" + face_ids + "]}";
        LCLogger.info().withMessageKey("delface").withMessage("delface.param=>" + param).flush();
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, authorization);
        LCLogger.info().withMessageKey("delface").withMessage("delface.result=>" + resultStr).flush();
        exception(resultStr);
        return resultStr;
    }

    /**
     * 设置信息
     * 设置 person 的 name
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     * @param authorization 签名
     * @param appId         接入项目的唯一标识
     * @param person_id
     * @param person_name
     * @return
     * @throws Exception
     */
    public static String setinfo(String authorization, String appId, String person_id, String person_name) throws Exception {
        if (StringUtils.isBlank(authorization)) {
            throw new ServiceException("YOUTU-015");
        }
        if (StringUtils.isBlank(appId)) {
            throw new ServiceException("YOUTU-016");
        }
        if (StringUtils.isBlank(person_id)) {
            throw new ServiceException("YOUTU-020");
        }
        if (StringUtils.isBlank(person_name)) {
            throw new ServiceException("YOUTU-022");
        }
        String url = "https://service.image.myqcloud.com/face/setinfo";
        String param = "{\"appid\":\"" + appId + "\",\"person_id\": \"" + person_id + "\",\"person_name\":\"" + person_name + "\"}";
        LCLogger.info().withMessageKey("setinfo").withMessage("setinfo.param=>" + param).flush();
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, authorization);
        LCLogger.info().withMessageKey("setinfo").withMessage("setinfo.result=>" + resultStr).flush();
        exception(resultStr);
        return resultStr;
    }

    /**
     * 人脸检测与分析
     * 检测给定图片中的所有人脸(Face)的位置和相应的面部属性，
     * 位置包括(x, y, w, h)，面部属性包括性别( gender ), 年龄( age ), 表情( expression ),
     * 魅力( beauty ), 眼镜( glass )和姿态 (pitch，roll，yaw )
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     * @param authorization 签名
     * @param appId         接入项目的唯一标识
     * @param imgUrl        图片访问链接
     * @return
     * @throws Exception
     */
    public static String faceDetect(String authorization, String appId, String imgUrl) throws Exception {
        if (StringUtils.isBlank(authorization)) {
            throw new ServiceException("YOUTU-015");
        }
        if (StringUtils.isBlank(appId)) {
            throw new ServiceException("YOUTU-016");
        }
        if (StringUtils.isBlank(imgUrl)) {
            throw new ServiceException("YOUTU-018");
        }
        String url = "https://service.image.myqcloud.com/face/detect";
        String param = "{\"appid\":\"" + appId + "\",\"mode\": 1,\"url\":\"" + imgUrl + "\"}";
        LCLogger.info().withMessageKey("faceDetect").withMessage("faceDetect.param=>" + param).flush();
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, authorization);
        LCLogger.info().withMessageKey("faceDetect").withMessage("faceDetect.result=>" + resultStr).flush();
        exception(resultStr);
        return resultStr;
    }

    /**
     * 人脸对比 本接口用于计算两个 Face 的相似性以及五官相似度
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     * @param authorization 签名
     * @param appId         接入项目的唯一标识
     * @param imgUrlA       图片访问链接
     * @param imgUrlB       图片访问链接
     * @return
     * @throws Exception
     */
    public static String faceCompare(String authorization, String appId, String imgUrlA, String imgUrlB) throws Exception {
        if (StringUtils.isBlank(authorization)) {
            throw new ServiceException("YOUTU-015");
        }
        if (StringUtils.isBlank(appId)) {
            throw new ServiceException("YOUTU-016");
        }
        if (StringUtils.isBlank(imgUrlA) || StringUtils.isBlank(imgUrlB)) {
            throw new ServiceException("YOUTU-023");
        }
        String url = "https://service.image.myqcloud.com/face/compare";
        String param = "{\"appid\":\"" + appId + "\",\"urlA\": \"" + imgUrlA + "\", \"urlB\":\"" + imgUrlB + "\"}";
        LCLogger.info().withMessageKey("faceCompare").withMessage("faceCompare.param=>" + param).flush();
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, authorization);
        LCLogger.info().withMessageKey("faceCompare").withMessage("faceCompare.result=>" + resultStr).flush();
        exception(resultStr);
        return resultStr;
    }

    /**
     * 人脸验证接口 给定一个图片和一个 Person ，检查是否是同一个人
     *
     * @author ouyangzhenqiang
     * @date 2018/12/10
     * @param authorization 签名
     * @param appId         接入项目的唯一标识
     * @param personId      验证人Id
     * @param faceUrl       验证人脸Url
     * @return
     * @throws Exception
     */
    public static String faceVerify(String authorization, String appId, String personId, String faceUrl) throws Exception {
        if (StringUtils.isBlank(authorization)) {
            throw new ServiceException("YOUTU-015");
        }
        if (StringUtils.isBlank(appId)) {
            throw new ServiceException("YOUTU-016");
        }
        if (StringUtils.isBlank(personId)) {
            throw new ServiceException("YOUTU-024");
        }
        if (StringUtils.isBlank(faceUrl)) {
            throw new ServiceException("YOUTU-025");
        }
        String url = "https://service.image.myqcloud.com/face/verify";
        String param = "{\"appid\":\"" + appId + "\",\"person_id\": \"" + personId + "\", \"url\":\"" + faceUrl + "\"}";
        LCLogger.info().withMessageKey("faceVerify").withMessage("faceVerify.param=>" + param).flush();
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, authorization);
        LCLogger.info().withMessageKey("faceVerify").withMessage("faceVerify.result=>" + resultStr).flush();
        exception(resultStr);
        return resultStr;
    }

    /**
     * 接口异常处理
     *
     * @author ouyangzhenqiang
     * @date 2018/11/29
     * @param jsonResult
     * @throws Exception
     */
    public static void exception(String jsonResult) throws Exception {
        String code = "";
        JSONObject jsonObj = JSONObject.parseObject(jsonResult);
        code = jsonObj.getString("code");
        String zero = "0";
        if (!zero.equals(code)) {
            throw new Exception(jsonObj.getString("message"));
        }
    }
}
